package fgl.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * @author fuguangli
 * @description 前沿类
 * @Create date:    2017/7/10
 */
@Controller
public class DownloadController {

    @RequestMapping("downloadDemo")
    public void downDemo(HttpServletResponse response) {
        File file = new File("D:\\Program Files (x86)\\JetBrains\\IDEA WORKSPACE-DEMO\\myspringboot\\log\\boot.log");
        try {
            response.addHeader("content-Disposition", "attachment;filename=" + new String((file.getName() + "好人阿斯蒂芬阿法撒旦阿什顿撒地方").getBytes("utf-8"),"iso-8859-1"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.addHeader("Content-Length", file.length() + "");
        OutputStream out = null;
        InputStream in = null;
        try {
            in = new BufferedInputStream(new FileInputStream(file));
            byte[] buffer = new byte[(int) file.length()];
            in.read(buffer);
            out = new BufferedOutputStream(response.getOutputStream());
            out.write(buffer);
            out.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            System.err.println("客户端取消下载！");
        } finally {
            try {
                in.close();
                out.close();
            } catch (IOException e) {
            }
        }
    }
}
